package com.elitel.frame.base.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.service.BaseQueryfieldService;
import com.elitel.frame.base.service.BaseRepparmeterService;
import com.elitel.frame.base.service.CfgExtreportService;
import com.elitel.frame.core.util.FileControlUtil;
import com.elitel.frame.main.dao.BaseRepdatasourceMapper;
import com.elitel.frame.main.dao.BaseRepparmeterMapper;
import com.elitel.frame.main.dao.BaseReptemplateMapper;
import com.elitel.frame.main.dao.CfgExtreportMapper;
import com.elitel.frame.main.dao.SysPrjserviceMapper;
import com.elitel.frame.main.dao.ext.ExtBaseRepdatasourceMapper;
import com.elitel.frame.main.dao.ext.ExtBaseRepparmeterMapper;
import com.elitel.frame.main.dao.ext.ExtBaseReptempletMapper;
import com.elitel.frame.main.dao.ext.ExtCfgExtreportMapper;
import com.elitel.frame.main.entity.BaseRepdatasource;
import com.elitel.frame.main.entity.BaseRepparmeter;
import com.elitel.frame.main.entity.BaseReptemplate;
import com.elitel.frame.main.entity.CfgExtreport;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.BaseQueryfieldExt;
import com.elitel.frame.main.entity.ext.CfgExtreportExt;

@Service
public class CfgExtreportServiceImpl implements CfgExtreportService{
	
	@Autowired
	private CfgExtreportMapper cfgExtreportMapper;
	@Autowired
	private ExtCfgExtreportMapper extCfgExtreportMapper;
	@Autowired
	private SysPrjserviceMapper sysPrjserviceMapper;
	@Autowired
	private ExtBaseRepdatasourceMapper extBaseRepdatasourceMapper;
	@Autowired
	private ExtBaseRepparmeterMapper extBaseRepparmeterMapper;
	@Autowired
	private ExtBaseReptempletMapper extBaseReptemplateMapper;
	@Autowired
	private BaseRepparmeterMapper baseRepparmeterMapper;
	@Autowired
	private BaseReptemplateMapper baseReptemplateMapper;
	@Autowired
	private BaseRepdatasourceMapper baseRepdatasourceMapper;
	@Autowired
	private BaseRepparmeterService baseRepparmeterService;
	@Autowired
	private BaseQueryfieldService baseQueryfieldService;
	@Autowired
	private  HttpServletRequest request;
	
	@Override
	public CfgExtreport getReportConfigByGuid(String guid) {
		CfgExtreport reportconfig = cfgExtreportMapper.selectByPrimaryKey(guid);
		return reportconfig;
	}

	@Override
	public Integer insertCfgExtreport(CfgExtreportExt cfgExtreportext) {
		SysPrjservice prjservice = new SysPrjservice();
		prjservice.setGuid(UUID.randomUUID().toString());
		prjservice.setPcode(cfgExtreportext.getPcode());
		prjservice.setServicetype("reportServer");
		prjservice.setServicecode(cfgExtreportext.getGuid());
		sysPrjserviceMapper.insertSelective(prjservice);
		int size = extCfgExtreportMapper.insertSelective(cfgExtreportext);
		return size;
	}

	@Override
	public Integer deleteCfgExtreport(String guid) {
		int size = cfgExtreportMapper.deleteByPrimaryKey(guid);
		return size;
	}

	@Override
	public Integer updateCfgExtreport(CfgExtreport cfgExtreport) {
		int size = cfgExtreportMapper.updateByPrimaryKeySelective(cfgExtreport);
		return size;
	}

	@Override
	public void deleteReportServerByPrjservice(SysPrjservice prjservice) throws Exception {
		Integer size=0;
		//删除主服务表
		sysPrjserviceMapper.deleteByPrimaryKey(prjservice.getGuid());
		if(null != prjservice.getServicecode() && !"".equals(prjservice.getServicecode())) {
			//删除服务配置
			cfgExtreportMapper.deleteByPrimaryKey(prjservice.getServicecode());
			//删除数据源配置
			extBaseRepdatasourceMapper.deleteDBConfigByServiceid(prjservice.getServicecode());
			//删除参数配置
			extBaseRepparmeterMapper.deleteParmeterByServiceid(prjservice.getServicecode());
			//删除模板文件
			BaseReptemplate template = extBaseReptemplateMapper.getReptemplateByServiceid(prjservice.getServicecode());
			//删除模板配置
			String jasperurl = request.getSession().getServletContext().getRealPath(template.getTemplateUrl());
			String jrxmlurl = request.getSession().getServletContext().getRealPath(template.getJrxmlUrl());
			if(jasperurl != null && jasperurl != ""){
				FileControlUtil.deleteFile(jasperurl);
			}
			if(jrxmlurl != null && jrxmlurl != ""){
				FileControlUtil.deleteFile(jrxmlurl);
			}
			extBaseReptemplateMapper.deleteTemplateByServiceid(prjservice.getServicecode());
		}
	}

	@Override
	public CfgExtreportExt getReportConfigInformationByGuid(String guid) {
		CfgExtreportExt reportconfig = extCfgExtreportMapper.getReportConfigInformationByGuid(guid);
		return reportconfig;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int insertReportAllConfig(HttpServletRequest httpServletRequest) {
		int size = 0;
		HttpSession session = httpServletRequest.getSession();
		//服务基本信息
		CfgExtreportExt cfgExtreport = (CfgExtreportExt) session.getAttribute("reportBaseInformation");
		SysPrjservice prjservice = new SysPrjservice();
		prjservice.setGuid(UUID.randomUUID().toString());
		prjservice.setPcode(cfgExtreport.getPcode());
		prjservice.setServicetype("reportServer");
		prjservice.setServicecode(cfgExtreport.getGuid());
		sysPrjserviceMapper.insertSelective(prjservice);
		size += extCfgExtreportMapper.insertSelective(cfgExtreport);
//		size = cfgExtreportMapper.insertSelective(cfgExtreport);
		//报表模板信息
		String jasperurl = session.getAttribute("reportJasperUrl").toString();
		String jrxmlurl = session.getAttribute("reportJrxmlUrl").toString();
		BaseReptemplate btemp = new BaseReptemplate();
		btemp.setGuid(UUID.randomUUID().toString());
		btemp.setServicecode(cfgExtreport.getGuid());
		btemp.setJrxmlUrl(jrxmlurl);
		btemp.setTemplateUrl(jasperurl);
		size += baseReptemplateMapper.insertSelective(btemp);
		//报表数据源服务配置
		List<BaseRepdatasource> dblist = (List<BaseRepdatasource>) session.getAttribute("reportDataServerList");
		for (BaseRepdatasource baseRepdatasource : dblist) {
			size+=baseRepdatasourceMapper.insertSelective(baseRepdatasource);
		}
		//报表服务属性参数
		List<BaseRepparmeter> replist = (List<BaseRepparmeter>) session.getAttribute("reportParmeterList");
		for (BaseRepparmeter baseRepparmeter : replist) {
			size += baseRepparmeterMapper.insertSelective(baseRepparmeter);
		}
		//服务数据源配置参数
		List<BaseRepparmeter> dbparamlist = (List<BaseRepparmeter>) session.getAttribute("dataServerParmeterList");
		for (BaseRepparmeter baseRepparmeter : dbparamlist) {
			size += baseRepparmeterMapper.insertSelective(baseRepparmeter);
		}
		return size;
	}

	@Override
	public int updateReportAllConfig(HttpServletRequest httpServletRequest) {
		int size = 0;
		HttpSession session = httpServletRequest.getSession();
		//服务基本信息
		CfgExtreportExt cfgExtreport = (CfgExtreportExt) session.getAttribute("reportBaseInformation");
		CfgExtreport extreport = new CfgExtreport();
		extreport.setGuid(cfgExtreport.getGuid());
		extreport.setIspagination(cfgExtreport.getIspagination());
		extreport.setReportdesc(cfgExtreport.getReportdesc());
		extreport.setServicename(cfgExtreport.getServicename());
		extreport.setShowtype(cfgExtreport.getShowtype());
		size += cfgExtreportMapper.updateByPrimaryKeySelective(extreport);
		//报表模板信息
		if(null != session.getAttribute("reportJasperUrl") && null != session.getAttribute("reportJrxmlUrl")) {
			String jasperurl = session.getAttribute("reportJasperUrl").toString();
			String jrxmlurl = session.getAttribute("reportJrxmlUrl").toString();
			BaseReptemplate btemp = new BaseReptemplate();
			btemp.setGuid(UUID.randomUUID().toString());
			btemp.setServicecode(cfgExtreport.getGuid());
			btemp.setJrxmlUrl(jrxmlurl);
			btemp.setTemplateUrl(jasperurl);
			size += extBaseReptemplateMapper.updateExtBaseReptemplate(btemp);
		}
		//报表数据源服务配置
		List<BaseRepdatasource> dblist = (List<BaseRepdatasource>) session.getAttribute("reportDataServerList");
		extBaseRepdatasourceMapper.deleteDBConfigByServiceid(cfgExtreport.getGuid());
		for (BaseRepdatasource baseRepdatasource : dblist) {
			size+=baseRepdatasourceMapper.insertSelective(baseRepdatasource);
		}
		//报表服务属性参数
		List<BaseRepparmeter> replist = (List<BaseRepparmeter>) session.getAttribute("reportParmeterList");
		extBaseRepparmeterMapper.deleteParmeterByServiceid(cfgExtreport.getGuid());
		for (BaseRepparmeter baseRepparmeter : replist) {
			size += baseRepparmeterMapper.insertSelective(baseRepparmeter);
		}
		//服务数据源配置参数
		List<BaseRepparmeter> dbparamlist = (List<BaseRepparmeter>) session.getAttribute("dataServerParmeterList");
		for (BaseRepparmeter baseRepparmeter : dbparamlist) {
			size += baseRepparmeterMapper.insertSelective(baseRepparmeter);
		}
		return size;
	}

	@Override
	public List<Map<String, String>> queryReportQueryParams(String serviceID) {
		List<Map<String,String>> result = new ArrayList<>();
		List<BaseRepparmeter> list = baseRepparmeterService.getReportParmToQuery(serviceID,"dataserverparmeter","0");
		if(null != list && list.size() > 0) {
			for (BaseRepparmeter br : list) {
				BaseQueryfieldExt queryfield = baseQueryfieldService.getQueryFieldByParamCode(br.getDataservercode(),br.getParmetername());
				Map<String, String> map = new LinkedHashMap<String,String>();
				if(null!=br.getAliasname() && !"".equals(br.getAliasname())) {
					map.put("fieldname", br.getAliasname());
				} else {
					map.put("fieldname", br.getParmetername());
				}
				map.put("fieldnamecn",queryfield.getFieldnameCn());
				map.put("fieldtype",queryfield.getFieldtype());
				map.put("showtype",queryfield.getShowtype().toString());
				map.put("enumvalue",queryfield.getEnumvalue());
				result.add(map);
			}
		}
		List<BaseRepparmeter> replist = baseRepparmeterService.getReportParmToQuery(serviceID,"templateparmeter","0");
		if(null != replist && replist.size() > 0 ) {
			for (BaseRepparmeter rep : replist) {
				Map<String, String> map = new LinkedHashMap<String,String>();
				if(null!=rep.getAliasname() && !"".equals(rep.getAliasname())) {
					map.put("fieldname", rep.getAliasname());
				} else {
					map.put("fieldname", rep.getParmetername());
				}
				map.put("fieldnamecn",rep.getParmeterdesc());
				map.put("fieldtype",rep.getParmetertype());
				map.put("showtype","0");
				result.add(map);
			}
		}
		return result;
	}

}
